package com.yjhb.android.sbz.adapter;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.yjhb.android.sbz.R;
import com.yjhb.android.sbz.entity.SystemAnnouncement;
import com.yjhb.android.sbz.v2.activity.NoticeDetailActivity;
import com.jojo.android.zxlib.base.BaseRecyclerAdapter;
import com.jojo.android.zxlib.tool.ZxTimeTool;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 创建者 zx
 * 创建时间 2021/1/11 11:16
 */
public class SystemAnnouncementAdapter extends BaseRecyclerAdapter<SystemAnnouncementAdapter.Holder, SystemAnnouncement> {
    private final String TAG = "SystemNoticeAdapter";


    public SystemAnnouncementAdapter(Context context) {
        super(context);
    }

    @Override
    protected Holder getHolder(View v) {
        return new Holder(v);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.item_system_notice;
    }

    @Override
    public void onBindViewHolder(@NonNull Holder holder, int position) {
        holder.contentTv.setText(entity.getMessage());
        holder.contentTv.post(new Runnable() {
            @Override
            public void run() {
                int count = holder.contentTv.getLayout().getEllipsisCount(holder.contentTv.getLineCount() - 1);
                if (count > 0) {
                    holder.line.setVisibility(View.VISIBLE);
                    holder.more_layout.setVisibility(View.VISIBLE);
                } else {
                    holder.line.setVisibility(View.GONE);
                    holder.more_layout.setVisibility(View.GONE);
                }
            }
        });

        holder.dateTv.setText(ZxTimeTool.getDate(entity.getCreateTime().getTime(), "yyyy-MM-dd HH:mm"));
        if (TextUtils.isEmpty(entity.getTittle())) {
            holder.titleTv.setText("系统公告");
        } else {
            holder.titleTv.setText(entity.getTittle());
        }
        holder.more_layout.setOnClickListener(v -> {
            Intent intent = new Intent(context, NoticeDetailActivity.class);
            intent.putExtra("notice", data.get(position));
            context.startActivity(intent);
        });
    }


    class Holder extends RecyclerView.ViewHolder {
        @BindView(R.id.title_tv)
        TextView titleTv;
        @BindView(R.id.date_tv)
        TextView dateTv;
        @BindView(R.id.content_tv)
        TextView contentTv;
        @BindView(R.id.more_layout)
        LinearLayout more_layout;
        @BindView(R.id.line)
        View line;

        public Holder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
//            contentTv.setTrimCollapsedText("展开");
//            contentTv.setTrimExpandedText("收起");
        }
    }

}
